//
//  SettingAViewController.m
//  MagazineShangejie
//
//  Created by wen on 16/3/11.
//  Copyright © 2016年 CnbizMedia. All rights reserved.
//

//---------------------------------------------------------
//                    *              *
//                  *      账号界面     *
//                    *              *
//---------------------------------------------------------
#import "SettingAViewController.h"
//#import "SettingTableViewController.h"
#import "Util.h"
#import "UILabel+CustomBadge.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "WebViewController.h"
#import "OpinionViewController.h"
#import "RankViewController.h"
#import "ProblemViewController.h"
#import "UMSocial.h"
#import "ChooseVIPViewController.h"
#import "NEWChooseVIPViewController.h"
#import "QRSystemVC.h"
#import <OpenUDID.h>


//#import "ArticleViewController.h"
//#import "ProfileViewController.h"
 


@interface SettingAViewController () <UIApplicationDelegate>{
    NSMutableDictionary*dic_m;
    AppDelegate *app;
    UIView*headerView;
    NSMutableArray*headerButtons;
    
    
    __weak IBOutlet UITableView *aTable;
   
    BOOL IamVIP;
 
    float *alpha;
    UIView *bckView;
    
    
    __weak IBOutlet UIView *backViewColor;
}



@end

@implementation SettingAViewController
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title=@"账号";
    IamVIP = NO;
    
    
    
//    其次,请在viewdidload,或者加载视图的时候初始化bckView
    alpha = 0; // 初始化透明度
    UIView *backgroundView = [[self.navigationController valueForKey:@"_navigationBar"] valueForKey:@"_backgroundView"];
    backgroundView.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:0];// 颜色自己选
    backgroundView.alpha = 0; // 做渐变最好再设置一下
    bckView = backgroundView;
    
//    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"pngBJ"] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.alpha=0.0;
//    [self.navigationController setNavigationBarHidden:YES];
    
    NSString *openUdid = [OpenUDID_UDID value];
    NSLog(@"openUdid == %@",openUdid);
    
    
    headerButtons=[[NSMutableArray alloc]init];
    
    
    UIImageView *headBackImage = [[UIImageView alloc]init];
    
    
    
    NSString *iphoneTheHight = [UserDefaults objectForKey:@"iphoneTheHight"];
    if ([iphoneTheHight isEqualToString:@"iphone5"]) {
        [headBackImage sd_setImageWithURL:[UserDefaults objectForKey:@"headBack_NewYear5"] ];
        
    }
    if ([iphoneTheHight isEqualToString:@"iphone6"]) {
        [headBackImage sd_setImageWithURL:[UserDefaults objectForKey:@"headBack_NewYear6"] ];
    }
    if ([iphoneTheHight isEqualToString:@"iphone6p"]) {
        [headBackImage sd_setImageWithURL:[UserDefaults objectForKey:@"headBack_NewYear6p"] ];
    }
    
    //    [self.navigationController.navigationBar setBackgroundImage:headBackImage.image forBarMetrics:UIBarMetricsDefault];
    
    
    //判断是不是要放置新春版
    NSString *newOldYear = [UserDefaults objectForKey:@"NewYear" ];
    if ([newOldYear isEqualToString:@"NewYear"]) {
        
        NSString *iphoneTheHight = [UserDefaults objectForKey:@"iphoneTheHight"];
        if ([iphoneTheHight isEqualToString:@"iphone5"]) {
            [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"headBack_NewYear5"] forBarMetrics:UIBarMetricsDefault];
        }
        if ([iphoneTheHight isEqualToString:@"iphone6"]) {
            [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"headBack_NewYear6"] forBarMetrics:UIBarMetricsDefault];
        }
        if ([iphoneTheHight isEqualToString:@"iphone6p"]) {
            [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"headBack_NewYear6p"] forBarMetrics:UIBarMetricsDefault];
        }
        
        [self.navigationController.navigationBar setTitleTextAttributes:
         
         @{NSFontAttributeName:[UIFont systemFontOfSize:17],
           
           NSForegroundColorAttributeName:[UIColor whiteColor]}];
        
    }
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    aTable.backgroundColor= UIColorFromRGB(0xc1968c) ;//[UIColor whiteColor]
    backViewColor.backgroundColor = UIColorFromRGB(0xc1968c);
    //    [self initData];
    [self setRightBarButton];
    aTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    aTable.frame=CGRectMake (0,0,self.view.frame.size.width,self.view.frame.size.height-49);
    
    //    app = APPDelegate;
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    
    if ([app.BadgeArray count]==0) {
        return;
    }
    [app.BadgeArray[0] performSelector:@selector(setMyAppCustomBadgeValue:) withObject:@"0"];
    
    
    //接收通知的设置,接收各种目标在设置的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showBootomBar) name:@"showBootomBar" object:nil];//都是nil
    
    //接收通知的设置,刷新新的VIP的标示
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableHeader) name:@"showTheNewVIP" object:nil];//都是nil
    
}

- (void)showBootomBar{
    
    self.hidesBottomBarWhenPushed = NO;
}


-(void)setRightBarButton{
    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
    //    [button setImage:[UIImage imageNamed:@"setpro"] forState:UIControlStateNormal];
    
    UIImageView *souImg = [[UIImageView alloc]init];
    [button setImage:[UIImage imageNamed:@"souWhite"] forState:UIControlStateNormal];
    [souImg sd_setImageWithURL:[UserDefaults objectForKey:@"topuser"] placeholderImage:[UIImage imageNamed:@"setpro"]];
    
    [button setFrame:CGRectMake(0, 0, 40, 40)];
    [button addTarget:self action:@selector(RightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightBarItem = [[UIBarButtonItem alloc]initWithCustomView:button];
//    self.navigationItem.rightBarButtonItem=rightBarItem;
}

-(void)RightBarButtonAction:(id)obj{
    
    //    [self.navigationController setNavigationBarHidden:NO animated:true];
    
    if (![UserDefaults objectForKey:@"user"]) {
        UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LoginViewController*login=[story instantiateViewControllerWithIdentifier:@"LoginViewController"];
        [self.navigationController pushViewController:login animated:YES];
        
    }else{
        [self performSegueWithIdentifier:@"ToProfile" sender:self];
    }
}

-(UIView*)tableHeader{
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 320)];
    UIView*Bview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 230)];
    UIImageView *backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 203)];
    [view addSubview:backImage];
    backImage.image = [UIImage imageNamed:@"UserBackground"];
    //    Bview.backgroundColor=UIColorFromRGB(0xe60111);
    //    view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"UserBackground"]];
    //    view.backgroundColor = [UIColor greenColor];
    //通过groupid 判断用户是否是VIP
    [view addSubview:Bview];
    UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake((view.frame.size.width - 70)/ 2, 40, 70, 70)];
    [imageView.layer setCornerRadius:imageView.frame.size.width/2];
    [imageView.layer setMasksToBounds:YES];
    imageView.userInteractionEnabled=YES;
    UIButton*imagebutton=[UIButton buttonWithType:UIButtonTypeCustom];
    [imagebutton setFrame:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.width)];
    [imagebutton addTarget:self action:@selector(RightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:imagebutton];
    [view addSubview:imageView];
    UILabel*name=[[UILabel alloc]initWithFrame:CGRectMake((view.frame.size.width - 100)/ 2, imageView.frame.origin.y + 80, 100, 15)];
    name.font = [UIFont fontWithName:@"HelveticaNeue" size:13];
    name.textAlignment = NSTextAlignmentCenter;
    name.textColor=[UIColor whiteColor];
    
    UIButton *VIPUpButton = [[UIButton alloc] initWithFrame:CGRectMake((view.frame.size.width - 80)/ 2, name.frame.origin.y + 20, 80, 23)];
    [VIPUpButton setImage:[UIImage imageNamed:@"VIPUpButton"] forState:UIControlStateNormal];
    [VIPUpButton addTarget:self action:@selector(VIPUpButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel*signature=[[UILabel alloc]initWithFrame:CGRectMake(imageView.frame.size.width+imageView.frame.origin.x+20, 60, self.view.frame.size.width, 40)];
    signature.textColor=[UIColor whiteColor];
    //    UIImageView*VIPImageView=[[UIImageView alloc]initWithFrame:CGRectMake(name.frame.origin.x + name.frame.size.width + 15, name.frame.origin.y, 49, 15)];
    UIImageView*VIPImageView=[[UIImageView alloc]initWithFrame:CGRectMake(name.frame.origin.x + name.frame.size.width + 15 + 5, name.frame.origin.y + 12, 49, 15)];
    
    UIImageView*VIPmarkImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 75  , name.frame.origin.y - 5, 64, 26)];
    //    name.textAlignment=NSTextAlignmentCenter;
    UILabel *NONumber = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 85, 10, 80, 20)];
    NONumber.textColor = [UIColor whiteColor];
    //    NONumber.font = [UIFont fontWithName:@"STHeiti-Medium.ttc" size:8];
    NONumber.font = [UIFont systemFontOfSize:8];
    //    NONumber.text = @"acc";
    //    NONumber.backgroundColor = [UIColor redColor];
    
    UIView *fourButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, view.frame.size.height - 117, view.frame.size.width, 126)];
    fourButtonView.backgroundColor = [UIColor whiteColor];
    UILabel *vipPowerLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,2, 70, 30)];
    [vipPowerLabel setTintColor:[UIColor blackColor]];
    //    vipPowerLabel.backgroundColor = [UIColor greenColor];
    vipPowerLabel.text = @"会员尊享";
    UIImageView *lineImage = [[UIImageView alloc]initWithFrame:CGRectMake(14, vipPowerLabel.frame.origin.y + 33, 600, 0.5)];
    lineImage.backgroundColor = [UIColor lightGrayColor];
    [fourButtonView addSubview:lineImage];
    
    [fourButtonView addSubview:vipPowerLabel];
    
    
    
    UITapGestureRecognizer *tap=[[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (RightBarButtonAction:)];
    UITapGestureRecognizer *tap2=[[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (RightBarButtonAction:)];
    UITapGestureRecognizer *tap3=[[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (RightBarButtonAction:)];
    [view addGestureRecognizer :tap3];
    view. userInteractionEnabled = YES ;
    [name addGestureRecognizer :tap];
    name. userInteractionEnabled = YES ;
    [signature addGestureRecognizer :tap2];
    signature. userInteractionEnabled = YES ;
    
    //    [view addSubview:signature];
    [view addSubview:name];
    [view addSubview:VIPUpButton];
    //    [view addSubview:VIPImageView];
    //    [view addSubview:VIPmarkImageView];
    
    IamVIP = NO;//判断是不是会员，然后可以展示用户的权限
    if ([UserDefaults objectForKey:@"user"]) {
        
        name.text=[NSString stringWithFormat:@"%@",[[UserDefaults objectForKey:@"user"] objectForKey:@"nickname"]];
        signature.text=[NSString stringWithFormat:@"%@",[[UserDefaults objectForKey:@"user"] objectForKey:@"signature"]];
        NSString *url = [NSString stringWithFormat:@"%@",[UserDefaults objectForKey:@"user"][@"thumb"][@"180"]];
        
        NSLog(@"[UserDefaults objectForKey:@\"overduedate\"] %@",[UserDefaults objectForKey:@"overduedate"]);
        if ([[[UserDefaults objectForKey:@"user"] objectForKey:@"groupid"] isEqualToString:@"6"]) {
            //            view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"settableviewbg"]];
            [VIPImageView setImage:[UIImage imageNamed:@"pt_vip"]];
            //            .image = [UIImage imageNamed:@"pt_vip"];
        }
        
        else if ([[[UserDefaults objectForKey:@"user"] objectForKey:@"groupid"] isEqualToString:@"9"]) {
            IamVIP = YES;
            //            view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"vipbg"]];
            [VIPImageView setImage:[UIImage imageNamed:@"vip"]];
            [VIPmarkImageView setImage:[UIImage imageNamed:@"lbc"]];
            NONumber.text = [NSString stringWithFormat:@"NO:%@",[[UserDefaults objectForKey:@"user"] objectForKey:@"vip_sn"] ];
            
        }
        
        else if ([[[UserDefaults objectForKey:@"user"] objectForKey:@"groupid"] isEqualToString:@"10"]) {
            IamVIP = YES;
            //            view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"svipbg"]];
            [VIPImageView setImage:[UIImage imageNamed:@"svip"]];
            [VIPmarkImageView setImage:[UIImage imageNamed:@"lbc_2"]];
            NONumber.text = [NSString stringWithFormat:@"NO:%@",[[UserDefaults objectForKey:@"user"] objectForKey:@"vip_sn"] ];
            
        }
        
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:url] options:SDWebImageRefreshCached progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            if (image==nil) {
                [imageView setImage:[UIImage imageNamed:@"icon_default_ipad"]];
            }else{
                [imageView setImage:[CommonHelper reSizeImage:image toSize:CGSizeMake(60, 60)]];
            }
        }];
        
    }else{
        signature.text=@"看商界,悟人生!";
        [imageView setImage:[UIImage imageNamed:@"icon_default_ipad"]];
        name.text=@"请先登录";//...
    }
    float size = self.view.frame.size.width/4;
    
    
    
    
    NSMutableDictionary*dic_w=[NSMutableDictionary dictionaryWithDictionary:[UserDefaults objectForKey:@"user"]];//获取个人用户信息
    NSLog(@"dic_w ----%@",dic_w);

    for (int i =0;i<4;i++){
        UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(size*i, VIPUpButton.frame.origin.y + 20, size, 35)];
        //        [button setBackgroundColor:[UIColor orangeColor]];
        //        button.layer.borderWidth=0.5;
        //        button.layer.borderColor=[UIColor colorWithWhite:0.500 alpha:0.200].CGColor;
        NSString*imageName=[NSString stringWithFormat:@"settable%d",i+1];
        UIImageView*imagev=[[UIImageView alloc]initWithFrame:CGRectMake(size/2-12, 20, 25, 25)];
        //        imagev.image=[UIImage imageNamed:imageName];
        //        [button addSubview:imagev];
        //        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor clearColor]];
        //        [button setImageEdgeInsets:UIEdgeInsetsMake(-20, 0, 0, 0)];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        UIImageView *userLine = [[UIImageView alloc]initWithFrame:CGRectMake(size*i, VIPUpButton.frame.origin.y + 30, 0.5, 15)];
        userLine.image = [UIImage imageNamed:@"user_line"];
        
        /* @"等级"   原来的排序
         @"积分"
         @"收藏"
         @"评论"?*/
        if (i==0) {
            
            NSString *favorite = [[NSString alloc]init];
            if ( dic_w[@"favorite"] ) {
                 favorite = [NSString stringWithFormat:@"收藏 %@", dic_w[@"favorite"] ];
            }
            else {
                favorite = @"收藏";
            }
            
            [button setTitle:favorite forState:UIControlStateNormal];
            
        }else if (i==1){
//            NSString *comment = [NSString stringWithFormat:@"评论 %@", dic_w[@"comment"] ];
            
            NSString *comment = [[NSString alloc]init];
            if ( dic_w[@"comment"] ) {
                comment = [NSString stringWithFormat:@"评论 %@", dic_w[@"comment"] ];
            }
            else {
                comment = @"评论";
            }
            [button setTitle:comment forState:UIControlStateNormal];
            [view addSubview:userLine];
            
        }else if (i==2){
//            NSString *vip = [NSString stringWithFormat:@"等级 %@", dic_w[@"vip"] ];
            
            NSString *vip = [[NSString alloc]init];
            if ( dic_w[@"vip"]  ) {
                vip = [NSString stringWithFormat:@"等级 %@", dic_w[@"vip"] ];
            }
            else {
                vip = @"等级";
            }
            
            [button setTitle:vip forState:UIControlStateNormal];
            [view addSubview:userLine];
        }else{
//            NSString *point = [NSString stringWithFormat:@"积分 %@", dic_w[@"point"] ];
            
            NSString *point = [[NSString alloc]init];
            if ( dic_w[@"point"] ) {
                point = [NSString stringWithFormat:@"积分 %@", dic_w[@"point"] ];
            }
            else {
                point = @"积分";
            }
            
            [button setTitle:point forState:UIControlStateNormal];
            [view addSubview:userLine];
        }
        
        [button setTitleColor:[UIColor colorWithRed:236 green:236 blue:236 alpha:1] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(headerButtonActon:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [button setFont:[UIFont systemFontOfSize:13]];
        //        [button setTitleEdgeInsets:UIEdgeInsetsMake(40, 00, 0, 0)];
        
        [view addSubview:button];
        [headerButtons addObject:button];
        
    }
    
    for (long int j=0;j<4;j++){
        
        
        UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
        //                    [button setBackgroundColor:[UIColor purpleColor]];
        [button setFrame:CGRectMake(size*j + 17, vipPowerLabel.frame.origin.y + 28, 60, 80)];
        NSArray *buttonNameArray = [NSMutableArray arrayWithObjects:@"媒体服务",@"智慧传播",@"资源整合",@"学习进修",nil];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        
        
        if (j==0) {
            [button setTitle:@"媒体服务" forState:UIControlStateNormal];
            
            [button setImage:[UIImage imageNamed:@"VIPCamera"] forState:UIControlStateNormal];
            if (IamVIP == NO) {
                [button setImage:[UIImage imageNamed:@"VIPCameraGary"] forState:UIControlStateNormal];
            }
        }else if (j==1){
            [button setTitle:@"智慧传播" forState:UIControlStateNormal];
            
            [button setImage:[UIImage imageNamed:@"VIPLight"] forState:UIControlStateNormal];
            if (IamVIP == NO) {
                [button setImage:[UIImage imageNamed:@"VIPLightGary"] forState:UIControlStateNormal];
            }
        }else if (j==2){
            [button setTitle:@"资源整合" forState:UIControlStateNormal];
            
            [button setImage:[UIImage imageNamed:@"VIPMoney"] forState:UIControlStateNormal];
            if (IamVIP == NO) {
                [button setImage:[UIImage imageNamed:@"VIPMoneyGary"] forState:UIControlStateNormal];
            }
        }else{
            [button setTitle:@"学习进修" forState:UIControlStateNormal];
            
            [button setImage:[UIImage imageNamed:@"VIPStudy"] forState:UIControlStateNormal];
            if (IamVIP == NO) {
                [button setImage:[UIImage imageNamed:@"VIPStudyGary"] forState:UIControlStateNormal];
            }
            
            
        }
        
        
        //            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:dic[@"description"]] options:SDWebImageRefreshCached progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        //            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        ////                [button setImage:[CommonHelper reSizeImage:image toSize:CGSizeMake(45, 45)] forState:UIControlStateNormal];
        //                [button setImage:image forState:UIControlStateNormal];
        //            }];
        //                NSLog(@"++++++%@",dic);
        
        
        
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, 7.5, 0, 0)];
        
        //给button设置title
        [button setTitleEdgeInsets:UIEdgeInsetsMake(60, -40, 0, 0)];
        NSDictionary *attribs = @{NSFontAttributeName: [UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor lightGrayColor]};
        NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:buttonNameArray[j] attributes:attribs];
        NSLog(@"attributedText = %@",attributedText);
        [button setAttributedTitle:attributedText forState:UIControlStateNormal];
        [button setTag:j];
        [button addTarget:self action:@selector(mPm:) forControlEvents:UIControlEventTouchUpInside];
        [fourButtonView addSubview:button];
        
        [view addSubview:fourButtonView];
    }
    
    aTable.tableHeaderView.frame=CGRectMake(0, 0, self.view.frame.size.width + 100,500 );//view.frame.size.height
    aTable.tableHeaderView = view;
//    [aTable.tableHeaderView addSubview:view];
    headerView=view;
    
    return view;
}

- (void)mPm:(id)sender {
    UIButton*button=(UIButton*)sender;
    
    NSLog(@"button.tag == %ld",(long)button.tag);
    NSInteger buttonTag = button.tag;
    NSLog(@"buttonTag == %ld",(long)buttonTag);
}

//升级VIP
-(void)VIPUpButtonEvent{
    
}


-(void)headerButtonActon:(id)obj{
    
    //    [self.navigationController setNavigationBarHidden:NO animated:true];
    
    ///等级、积分、收藏、评论
    if (![UserDefaults objectForKey:@"user"]) {
        UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LoginViewController*login=[story instantiateViewControllerWithIdentifier:@"LoginViewController"];//ZiXunBaoMingIdentifer     LoginViewController
        [self.navigationController pushViewController:login animated:YES];//YES
        return;
    }
    
    else{
        UIButton*button=(UIButton*)obj;
        if ([button tag]==2) {
            UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
            RankViewController*web=[story instantiateViewControllerWithIdentifier:@"RankViewController"];
            
            [self.navigationController pushViewController:web animated:YES];//YES
        }else if ([button tag]==3){
            UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
            WebViewController*web=[story instantiateViewControllerWithIdentifier:@"WebViewController"];
            web.url=@"http://ksjv2.kanshangjie.com/User/HelpPoint";
            [self.navigationController pushViewController:web animated:YES];//YES
            //        [self performSegueWithIdentifier:@"setPweb" sender:self];
            
        }else if ([button tag]==0){
            
            [self performSegueWithIdentifier:@"setPfavorite" sender:self];
            
        }else{
            UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
            WebViewController*web=[story instantiateViewControllerWithIdentifier:@"WebViewController"];
            web.title=@"我的评论";
            web.url=@"http://ksjv2.kanshangjie.com/Comment/MyComment";
            [self.navigationController pushViewController:web animated:YES];//YES
            
            //        [self performSegueWithIdentifier:@"setPweb" sender:self];
            
        }
    }
}
-(void)reloadHeaderButtonTitle{
    ///获取到数据到刷新 等级、积分、收藏、评论
    if (headerButtons.count>1) {
        for (UIButton*but in headerButtons) {
            
        }
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    ///清除headerView
    [headerButtons removeAllObjects];
    headerView=nil;
    
 bckView.alpha = 1.0;//隐藏的导航栏显示
    
    
 }

// 防止有时候其他界面的alpha变为0.5
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    bckView.alpha = 1.0;//隐藏的导航栏显示
  
}

-(void)delayMethod{

    //iOS 动画是UIView在动
    
    [UIView animateWithDuration:0.3 animations:^(void){
        
            bckView.alpha = 0;
                        } completion:^(BOOL finished) {
            bckView.alpha = 0;
    }];
    

}

-(void)viewDidAppear:(BOOL)animated{
    
}

//刷新界面
-(void)viewWillAppear:(BOOL)animated{
    
//        self.navigationController.navigationBar.alpha=0.0;
    //    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"pngBJ"] forBarMetrics:UIBarMetricsDefault];
//        [self.navigationController setNavigationBarHidden:YES animated:YES];
    //    [self.navigationController setNavigationBarHidden:NO animated:animated];
//    self.navigationController.navigationBar.alpha=0;
    [super viewWillAppear:animated];
    
    [self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:0.3];//延迟显示导航栏
    
    [self tableHeader];
    
    NSMutableDictionary*usDic=[NSMutableDictionary dictionaryWithDictionary:[UserDefaults objectForKey:@"usDic"]];
    if ([usDic count] > 1) {
        
        
        NSLog(@"usDic ----%@",usDic);
        [[AppAPIClient sharedClient] POST:@"User/Login" parameters:@{@"username":[usDic objectForKey:@"username"],@"password":[usDic objectForKey:@"password"]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *user = responseObject[@"data"];
            
            if (![(NSDictionary *)user isEqual: [NSNull null] ] ) {
                
                NSString *token = user[@"token"];
                //            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
                NSString *overduedate = user[@"overduedate"];//时间戳
                
                [UserDefaults setObject:user forKey:@"user"];
                [UserDefaults setObject:token forKey:@"token"];
                [UserDefaults setObject:overduedate forKey:@"overduedate"];
                
                
                
                
                
                
                //            [alert setMessage:@"登录成功！"];
                //                [MobClick profileSignInWithPUID:@"playerID" provider:@"WX"];//友盟登录统计
            }
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
    }
    [aTable reloadData];
}
-(void)initData{
    dic_m=[[NSMutableDictionary alloc]init];
    [[AppAPIClient sharedClient] GET:@"AppData/Activity" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        dic_m=[NSMutableDictionary dictionaryWithDictionary:responseObject];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // 更新界面
            [aTable reloadData];
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:LOADING_TEXT_ERROR];
        NSLog(@"Log:%@",operation.responseString);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    if (section==0) {
////        return [self tableHeader];
//        aTable.tableHeaderView = headerView;
//        return headerView;
//        

    
//    aTable.tableHeaderView = headerView;
//    if (section > 0) {
//        UIView *littleHeadView = [[UIView alloc]init];
//        littleHeadView.backgroundColor = [UIColor lightTextColor];
//        return littleHeadView;
//    }
//    else{
//        return nil;
//    }


//    UIView *littleHeadView = [[UIView alloc]init];
//    littleHeadView.backgroundColor = [UIColor lightTextColor];
//    return littleHeadView;
    
//    if (section==0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width, tableView.sectionHeaderHeight)];
        
        view.backgroundColor =  [[UIColor alloc] initWithRed:240/255.f green:240/255.f blue:240/255.f alpha:1];
        return view;

//    }
    
//    else{
//        UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
//        
//        UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//        headerLabel.backgroundColor = [UIColor clearColor];
//        headerLabel.opaque = NO;
//        headerLabel.textColor = [UIColor lightGrayColor];
//        headerLabel.highlightedTextColor = [UIColor whiteColor];
//        headerLabel.font = [UIFont boldSystemFontOfSize:20];
//        headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
//        
//        if (section == 0) {
//            headerLabel.text =  @"测试1";
//        }else if (section == 1){
//            headerLabel.text = @"测试2";
//        }else if (section == 2){
//            headerLabel.text = @"测试3";
//        }else if (section == 3){
//            headerLabel.text = @"测试4";
//        }
//        
//        [customView addSubview:headerLabel];
//        
////        return customView;
//        return nil;
//    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 8;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //#warning Incomplete method implementation.
    //     Return the number of rows in the section.
    
    if (section==0) {
        return 2;
    }
    else if (section==1) {
        return 1;
    }else if (section==2){
        return 1;
    }else if (section==3){
        //        return 2;
        return 1;
    }
    else if (section==4){
        //        return 2;
        return 1;
    }
    
    //        else if (section==5){
    //            //        return 2;
    //
    //            return 1;
    //        }
    
    else{
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//        if (indexPath.section==0) {
//            return 320;
//        }else{
    return 45;
//        }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        //
        
        return 15;
//        return 320;
        //    }else if (section==1){
        //
        //        return 10;
        //    }else if (section==2){
        //
        //        return 10;
        //    }else if (section==3){
        //
        //        return 10;
    }else{
        return 15;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell ;
    //    if(indexPath.section==0){
    //        cell = [tableView dequeueReusableCellWithIdentifier:@"setcellaa" forIndexPath:indexPath];
    //    }else{
    cell = [tableView dequeueReusableCellWithIdentifier:@"setcell" forIndexPath:indexPath];
    //    }
    if (cell==nil) {
        //        if(indexPath.section==0){
        //            cell=[tableView dequeueReusableCellWithIdentifier:@"setcellaa"];
        //        }else{
        //            cell=[tableView dequeueReusableCellWithIdentifier:@"setcell"];
        
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"setcell"];
        //        }
    }
    
    UIImageView*imageView=(UIImageView*)[cell.contentView viewWithTag:11];
    UILabel*label=(UILabel*)[cell.contentView viewWithTag:22];
    
    //    if (indexPath.section==0){
    //        if (indexPath.row==0) {
    //            imageView.image=[UIImage imageNamed:@"vipbtn"];
    //            label.text=@"查看VIP特权";
    //
    //
    //        }}
    //
    //    else
    
//    cell.backgroundColor = [UIColor lightGrayColor];
    
    
    if (indexPath.section==0){
        if (indexPath.row==0) {
            imageView.image=[UIImage imageNamed:@"userMessageIcon"];
            label.text=@"我的消息";
            
            [label setMyAppCustomBadgeValue:app.BadgeStr];
            
        }else if (indexPath.row==1){
            imageView.image=[UIImage imageNamed:@"userPayIcon"];
            label.text=@"支付订单";
        }else if (indexPath.row==2){
            
            imageView.image=[UIImage imageNamed:@"club"];
            label.text=@"商界俱乐部";
            
        }
        
    }else if (indexPath.section==1){
        imageView.image=[UIImage imageNamed:@"userShareIcon"];
        label.text=@"把看商界分享给朋友";
        
    }else if (indexPath.section==2){
        imageView.image=[UIImage imageNamed:@"userTuCaoIcon"];
        label.text=@"吐槽";
        
    }
    else if (indexPath.section==4){
        imageView.image=[UIImage imageNamed:@"userSetIcon"];
        label.text=@"设置";
        
    }
    else if (indexPath.section==3){
        imageView.image=[UIImage imageNamed:@"userSysIcon"];
        label.text=@"扫一扫";
        
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];//点击后不会有阴影
    
    //    [self.navigationController setNavigationBarHidden:NO animated:true];
    //    if (indexPath.section==0){
    //        self.hidesBottomBarWhenPushed = YES;
    //        //        [self performSegueWithIdentifier:@"pushVIP" sender:self];
    //
    //        UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //        ChooseVIPViewController *choo=[story instantiateViewControllerWithIdentifier:@"ChooseVIPViewController"];
    //        //        opin.view.backgroundColor=[UIColor colorWithWhite:0.890 alpha:1.000];
    //        //        [opin opinionInWWW:1];
    //        [self.navigationController pushViewController:choo animated:YES];
    //
    //    }
    //    else
    
    if (indexPath.section==1){
        
        BDSocialShareContent *content = [BDSocialShareContent shareContentWithDescription:@"超百万企业家都在看的商业财经类阅读客户端" url:@"http://open.kanshangjie.com/popweixin.php" title:@"看商界"];
        //    [content addImageWithThumbImage:[UIImage imageNamed:@"icon_default_ipad"] imageUrl:self._Dict[@"thumb"]];
        UIImageView*imageview=[[UIImageView alloc]init];
        imageview.image=[UIImage imageNamed:@"icon"];
        [SVProgressHUD dismiss];
        
        
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:nil
                                          shareText:[NSString stringWithFormat:@"超百万企业家都在看的商业财经类阅读客户端"]
                                         shareImage:[UIImage imageNamed:@"icon"]
                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToQQ,UMShareToQzone,UMShareToWechatTimeline,UMShareToWechatSession,UMShareToTencent,UMShareToEmail,UMShareToSms,nil]
                                           delegate:self];
        
        [UMSocialData defaultData].extConfig.qqData.url = @"http://open.kanshangjie.com/popweixin.php";
        [UMSocialData defaultData].extConfig.qzoneData.url = @"http://open.kanshangjie.com/popweixin.php";
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = @"http://open.kanshangjie.com/popweixin.php";
        [UMSocialData defaultData].extConfig.wechatSessionData.url = @"http://open.kanshangjie.com/popweixin.php";
        [UMSocialData defaultData].extConfig.sinaData.urlResource.url = @"http://open.kanshangjie.com/popweixin.php";
        
        [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline]];
        
        
        
        
        
        
    }
    else if (indexPath.section==2){
        
        UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ProblemViewController*opin=[story instantiateViewControllerWithIdentifier:@"ProblemViewController"];
        opin.view.backgroundColor=[UIColor colorWithWhite:0.890 alpha:1.000];
        //        [opin opinionInWWW:1];
        [self.navigationController pushViewController:opin animated:YES];
        //        [self performSegueWithIdentifier:@"pushProblemView" sender:self];
        
    }
    else if (indexPath.section==3){
        self.hidesBottomBarWhenPushed = YES;
        UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        QRSystemVC *choo=[story instantiateViewControllerWithIdentifier:@"QRSystemVC"];
        
        [self.navigationController pushViewController:choo animated:YES];
        
    }
    else if (indexPath.section==4){
        
        [self performSegueWithIdentifier:@"setPset" sender:self];
        
    }
    
    else if (![UserDefaults objectForKey:@"user"]) {
        UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LoginViewController*login=[story instantiateViewControllerWithIdentifier:@"LoginViewController"];
        [self.navigationController pushViewController:login animated:YES];
        
    }else{
        if (indexPath.section==0) {
            if (indexPath.row==0) {
                [self performSegueWithIdentifier:@"setPmes" sender:self];
            }else if (indexPath.row==1){
                [self performSegueWithIdentifier:@"showPayTableView" sender:self];//setpay
            }
        }
    }
}

-(void)didSelectSocialPlatform:(NSString *)platformName withSocialData:(UMSocialData *)socialData
{
    if (platformName == UMShareToSina) {
        socialData.shareText = [NSString stringWithFormat:@"超百万企业家都在看的商业财经类阅读客户端 http://open.kanshangjie.com/popweixin.php " ];
    }
    else{
        socialData.shareText = [NSString stringWithFormat:@"超百万企业家都在看的商业财经类阅读客户端 "  ];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    UIViewController *destination = segue.destinationViewController;
    if([destination isKindOfClass:[WebViewController class]]){
        [destination setValue:@"http://ksjv2.kanshangjie.com/User/HelpPoint" forKey:@"url"];
        [destination setTitle:@"我的任务"];
        
    }
}


/** 正在滚动，控制滚动的时候一些控件的显示和消失 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"scrollView %@",scrollView);
//    NSLog(@"contentOffset x %f y %f",scrollView.contentOffset.x,scrollView.contentOffset.y );
//    NSLog(@"contentOffset w %f h %f",scrollView.contentSize.width,scrollView.contentSize.height);
//    NSLog(@"contentOffset l %f r %f",scrollView.contentInset.left,scrollView.contentInset.right);
//    
//    NSLog(@"scrollView.contentSize.height - scrollView.contentOffset.y =%f",scrollView.contentSize.height - scrollView.contentOffset.y - self.view.frame.size.height);
    
    float nowHeight = scrollView.contentSize.height - scrollView.contentOffset.y - self.view.frame.size.height + scrollView.frame.origin.y;
    
 
    if (scrollView.contentOffset.y > 0 ) {
 

        aTable.backgroundColor = [UIColor whiteColor];
    }
    
    
    if (scrollView.contentOffset.y < 0 ) {
        
 
        aTable.backgroundColor= UIColorFromRGB(0xc1968c) ;//[UIColor whiteColor]
        
    }
    
    if (scrollView.contentOffset.y > 20) {

//        bckView.alpha = 1.0;
    }
    else if (scrollView.contentOffset.y < 20){
//        bckView.alpha = 0.0;
    }
    
}


@end
